#ifndef LUNARQUERYDETAILS_H
#define LUNARQUERYDETAILS_H

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include "LunarSolarConverter.h"
#include "fuction3.h"

using namespace std;

const char* tiangan[] = { "甲","乙","丙","丁","戊","己","庚","辛","壬","癸" };
const char* dizhi[]   = { "子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥" };

class fuction1_1 {
public:
    void Query() {
        int year, month, day;
        cout << "请输入公历日期 (格式：YYYY MM DD): ";
        cin >> year >> month >> day;

        // 显示该月月历
        fuction3 calendar;
        calendar.Show(year, month);

        // 显示干支
        cout << "\n【干支信息】\n";
        cout << "干支：" << GanZhi(year) << endl;

        // 节气列表
        vector<pair<int, string>> terms = jieqi();

        // 计算今天是该年的第几天
        int current = dayOfYear(year, month, day);
        int yearDays = isLeapYear(year) ? 366 : 365;

        bool isTerm = false;
        string nextTerm = "";
        int minDiff = 9999;

        for (int i = 0; i < terms.size(); i++) {
            int termMonth = terms[i].first / 100;
            int termDay = terms[i].first % 100;
            int jintian = dayOfYear(year, termMonth, termDay);

            if (jintian == current) {
                cout << "\n今天是节气：" << terms[i].second << endl;
                isTerm = true;
                break;
            }

            int diff = 0;
            if (jintian > current) {
                diff = jintian - current;
            } else {
                diff = (yearDays - current) + jintian;
            }

            if (diff < minDiff) {
                minDiff = diff;
                nextTerm = terms[i].second;
            }
        }

        if (!isTerm) {
            cout << "\n今天不是节气，距离下一个节气 [" << nextTerm << "] 还有 " << minDiff << " 天。" << endl;
        }

        // 显示本月节气
        cout << "\n【" << year << "年" << month << "月 节气列表】\n";
        for (int i = 0; i < terms.size(); i++) {
            int mm = terms[i].first / 100;
            int dd = terms[i].first % 100;
            if (mm == month) {
                cout << dd << "日 - " << terms[i].second << endl;
            }
        }

        cout << "\n按任意键返回主菜单..." << endl;
        _getch();
    }
private:
    // 判断是否是闰年
    bool isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    // 计算某年月日是该年的第几天
    int dayOfYear(int year, int month, int day) {
        int daysInMonth[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
        if (isLeapYear(year)) daysInMonth[1] = 29;

        int total = 0;
        for (int i = 0; i < month - 1; i++) {
            total += daysInMonth[i];
        }
        total += day;
        return total;
    }

    string GanZhi(int year) {
        int idx = (year - 4) % 60;
        return string(tiangan[idx % 10]) + dizhi[idx % 12];
    }
    vector<pair<int, string>> jieqi() {
        vector<pair<int, string>> terms;
        terms.push_back(make_pair(106, "小寒"));
        terms.push_back(make_pair(120, "大寒"));
        terms.push_back(make_pair(204, "立春"));
        terms.push_back(make_pair(219, "雨水"));
        terms.push_back(make_pair(305, "惊蛰"));
        terms.push_back(make_pair(320, "春分"));
        terms.push_back(make_pair(404, "清明"));
        terms.push_back(make_pair(419, "谷雨"));
        terms.push_back(make_pair(505, "立夏"));
        terms.push_back(make_pair(520, "小满"));
        terms.push_back(make_pair(605, "芒种"));
        terms.push_back(make_pair(621, "夏至"));
        terms.push_back(make_pair(707, "小暑"));
        terms.push_back(make_pair(722, "大暑"));
        terms.push_back(make_pair(807, "立秋"));
        terms.push_back(make_pair(823, "处暑"));
        terms.push_back(make_pair(907, "白露"));
        terms.push_back(make_pair(922, "秋分"));
        terms.push_back(make_pair(1008, "寒露"));
        terms.push_back(make_pair(1023, "霜降"));
        terms.push_back(make_pair(1107, "立冬"));
        terms.push_back(make_pair(1122, "小雪"));
        terms.push_back(make_pair(1207, "大雪"));
        terms.push_back(make_pair(1221, "冬至"));
        return terms;
    }
};

#endif
